import api from '../../utils/api.js';
import cache from '../../utils/cache.js';
import http from '../../utils/http.js';
import tools from '../../utils/tools.js';
const Http = new http;
const app = getApp();
// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    questionDetail: [],
    commentList: [],
    answer: {},
    discuss: {},
    visible: false,
    loading: true,
    searchValue: "",
    tabDefaultValue: 0,
    image: 'https://tdesign.gtimg.com/mobile/demos/avatar1.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let questionId = options.id;
    if (options.searchValue != undefined) {
      this.setData({
        searchValue: options.searchValue
      })
    }

    this.getQuestionList(questionId);
    this.getCommentList(questionId);
  },
  onBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  onGoHome() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  handleMore() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  handleClick() {
    let visible = this.data.visible;
    if (visible == true) {
      this.setData({
        visible: false
      })
    }
  },
  onIconTap(e) {
    let questionId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/addComment/addComment?id=' + questionId
    })
  },
  getCommentList(questionId) {
    const _ts = this;
    Http.httpReq({
      url: api.getCommentList,
      data: {
        question_id: questionId
      }
    }).then(res => {
      console.log(res.data);
      if (res.data.code == 1) {
        res.data.data.forEach(element => {
          // 转化html
          element.content = app.towxml.toJson(
            element.content, // `markdown`或`html`文本内容
            'html' // `markdown`或`html`
          );
        });
        _ts.setData({
          commentList: res.data.data,
        })
      }
    })
  },
  tapComment(e) {
    let questionId = e.currentTarget.dataset.questionid;

    const _ts = this;
    Http.httpReq({
      url: api.toggleLike,
      data: {
        question_id: questionId
      },
      method: 'POST',
      success: res => {
        console.log(res)
        if (res.data.code == 1) {
          wx.showToast({
            title: '发表成功，待审核成功后可见',
            icon: 'none',
            duration: 2000,
            success: function () {
              _ts.getCommentList()
            }
          })
        }
      }
    })
  },
  getQuestionList(questionId) {
    const _ts = this;
    Http.httpReq({
      url: api.getQuestionDetail,
      data: {
        qid: questionId
      }
    }).then(ret => {
      let data = ret.data.data;
      _ts.setData({
        questionDetail: data,
        loading: false,
        answer: app.towxml.toJson(
          data.answer, // `markdown`或`html`文本内容
          'markdown' // `markdown`或`html`
        ),
        // visible: userInfo.is_vip == 1 ? false : (data.vip_view == 1 ? true : false),
      })
    })
  },
  handleNext(e) {
    wx.redirectTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  onTabsChange(e) {
    let optionId = e.detail.value;
    this.setData({
      tabDefaultValue: optionId
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})